package com.one.group.admin.controller;

import com.one.group.model.vo.MessageVO;
import com.one.group.result.Result;
import com.one.group.service.ChatHistoryService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/AiChat")
@Slf4j
@Tag(name = "Ai相关接口")
public class AIController {
//    @Autowired
//    private ChatModel chatModel;
//    @GetMapping("/deepseekChat/v1")
//    @Operation(summary = "deepseek聊天调用")
//    public String chat(@RequestParam String msg) {
//        return chatModel.call(msg);
//    }
//
//    @GetMapping("/deepseekChat/v2")
//    @Operation(summary = "deepseek聊天流式调用")
//    public Flux<String> chat2(@RequestParam String msg) {
//        Flux<String> stream = chatModel.stream(msg);
//        return stream;
//    }
//    @Autowired
//    private ChatClient chatClient;
//
//    @GetMapping("/chat/v1")
//    @Operation(summary = "deepseek聊天调用")
//    public String chatClient(@RequestParam String msg) {
//        return chatClient.prompt().user(msg)
//                .call().content();
//    }
//
//    @GetMapping("/chat/v2")
//    @Operation(summary = "deepseek聊天流式调用")
//    public Flux<String> chat2Client(@RequestParam String msg) {
//        Flux<String> stream = chatClient.prompt().user(msg)
//                .stream().content();
//        return stream;
//    }
//    @Autowired
//    private ChatClient chatClient;
//    @GetMapping("/chat/v1")
//    @Operation(summary = "聊天调用")
//    public String chat(@RequestParam String msg,String chatId) {
//        return chatClient.prompt().user(msg)
//            .advisors(a->a.param(ChatMemory.CONVERSATION_ID,chatId))
//            .call()
//            .content();
//    }
//
//    @GetMapping("/chat/v2")
//    @Operation(summary = "聊天流式调用")
//    public Flux<String> chat2(@RequestParam String msg,String chatId) {
//        Flux<String> stream = chatClient.prompt().user(msg)
//                .advisors(a->a.param(ChatMemory.CONVERSATION_ID,chatId))
//                .stream()
//                .content();
//        return stream;
//    }
    @Autowired
    private ChatClient chatClient;

    @Autowired
    private ChatHistoryService chatHistoryService;

    @GetMapping("/chat/v1")
    @Operation(summary = "聊天调用")
    public String chat(@RequestParam String msg,String chatId) {
        //1.保存会话的id
        chatHistoryService.save("chat",chatId);
        return chatClient.prompt().user(msg)
                .advisors(a->a.param(ChatMemory.CONVERSATION_ID,chatId))
                .call()
                .content();
    }

    @GetMapping("/chat/v2")
    @Operation(summary = "聊天流式调用")
    public Flux<String> chat2(@RequestParam String msg,String chatId) {

        //1.保存会话的id
        chatHistoryService.save("chat",chatId);

        Flux<String> stream = chatClient.prompt().user(msg)
                .advisors(a->a.param(ChatMemory.CONVERSATION_ID,chatId))
                .stream()
                .content();
        return stream;
    }
    @GetMapping("/history/{type}")
    public Result<List<String>> getChatIds(@PathVariable("type") String type) {
        return ResultUtil.success(chatHistoryService.getChatIds(type));
    }
    @Autowired
    private ChatMemory chatMemory;
    @GetMapping("/history/{type}/{chatId}")
    public Result<List<MessageVO>> getChatHistory(
            @PathVariable("type") String type,
            @PathVariable("chatId") String chatId) {
        List<Message> messages = chatMemory.get(chatId);
        if (messages == null) {
            return ResultUtil.success(List.of());
        }
        return ResultUtil.success(
                messages.stream().map(MessageVO::new).toList());
    }

}
